package com.example.boot1.conttoller;
import com.example.boot1.bean.BackDataBean;
import com.example.boot1.bean.BackPageDataBean;
import com.example.boot1.bean.RequestStaffBean;
import com.example.boot1.bean.VueStaff;
import com.example.boot1.service.VueStaffService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
* (vue_staff)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/vue")
public class VueStaffController {

    @Autowired
    private VueStaffService vueStaffService;

    @PostMapping("/addStaff")
    @ResponseBody
    public ResponseEntity<BackDataBean> addStaff(HttpServletRequest request,
                                                   @RequestBody VueStaff vueStaff) {
        ResponseEntity responseEntity = null;
        BackDataBean backDataBean = null;
        //前端输入校验
        //员工编号
        if (null==vueStaff.getStaffon()  || "".equals(vueStaff.getStaffon())){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入员工编号！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            return  responseEntity;
        }
        //员工名称
        if (null==vueStaff.getName()  || "".equals(vueStaff.getName())){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入名称！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            return  responseEntity;
        }
        //员工性别
        if (null==vueStaff.getSex()  || vueStaff.getSex().equals("")){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入员工性别！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            return  responseEntity;
        }
        //职位
        if (null==vueStaff.getPosition()  || vueStaff.getPosition().equals("") ){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入员工职位！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            return  responseEntity;
        }
        //是否离职
        if (null==vueStaff.getQuit()  || vueStaff.getQuit().equals("")){
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("请输入是否离职状态！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            return  responseEntity;
        }
        //插入逻辑
        try {

            VueStaff vs = vueStaffService.findByStaffNo(vueStaff.getStaffon());

            if ( null != vs){
                backDataBean = new BackDataBean();
                backDataBean.setCode(202);
                backDataBean.setMsg("员工编号已存在！");
                backDataBean.setResult(null);
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            }else{

                int utList = vueStaffService.insertSelective(vueStaff);

                backDataBean = new BackDataBean();
                backDataBean.setCode(200);
                backDataBean.setMsg("保存成功！");
                backDataBean.setResult(utList);
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);
            }
        }catch (Exception e) {

            backDataBean.setCode(202);
            backDataBean.setMsg("服务器错误！");
            backDataBean.setResult(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backDataBean);
            e.printStackTrace();
        }
        return responseEntity;
    }


    @GetMapping("/getStaff")
    @ResponseBody
    public ResponseEntity<BackDataBean> getStaff(HttpServletRequest request, Integer page){
        ResponseEntity responseEntity = null;
        BackPageDataBean backPageDataBean = null;
        BackDataBean backDataBean = null;

        int pageOffSetNum = page * 10;
        List<VueStaff> vclist = vueStaffService.getStaff(pageOffSetNum);

        if (null!= vclist ){
            if ( vclist.size()>0){

                backDataBean = new BackDataBean();
                backDataBean.setCode(200);
                backDataBean.setMsg("查询成功！");
                backDataBean.setResult(vclist);

                backPageDataBean = new BackPageDataBean(backDataBean);
                backPageDataBean.setPage(page);
                backPageDataBean.setTotal(vueStaffService.findAllTotal());
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backPageDataBean);

            }else{
                backDataBean = new BackDataBean();
                backDataBean.setCode(200);
                backDataBean.setMsg("查询成功！");
                backDataBean.setResult(new ArrayList<>());

                backPageDataBean = new BackPageDataBean(backDataBean);
                backPageDataBean.setPage(page);
                backPageDataBean.setTotal(0);
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backPageDataBean);
            }

        }else{
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("没有数据！");
            backDataBean.setResult(null);

            backPageDataBean = new BackPageDataBean(backDataBean);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backPageDataBean);
        }

        return responseEntity;
    }

    //是否在职
    @GetMapping("/isInservice")
    @ResponseBody
    public ResponseEntity<BackDataBean> isInservice(HttpServletRequest request, VueStaff vueStaff){

        ResponseEntity responseEntity = null;
        BackDataBean backDataBean = null;
        //修改是否在职状态
        int resultNum = vueStaffService.updateByPrimaryKeySelective(vueStaff);

        backDataBean = new BackDataBean();
        backDataBean.setCode(200);
        backDataBean.setMsg("修改成功！");
        backDataBean.setResult(resultNum);
        responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);

        return responseEntity;
    }
    // @RequestBody  String arrIds
    @PostMapping("/deleteStaff")
    @ResponseBody
    public ResponseEntity<BackDataBean> deleteStaff(HttpServletRequest request, @RequestBody RequestStaffBean requestStaffBean){

        ResponseEntity responseEntity = null;
        BackDataBean backDataBean = null;
        System.out.println(requestStaffBean);
        long resultNum = 0;
        //批量删除
        if (null!= requestStaffBean.getArrIds() && requestStaffBean.getArrIds().size()>0){
             resultNum = vueStaffService.deleteByIds(requestStaffBean.getArrIds());
        }
        backDataBean = new BackDataBean();
        backDataBean.setCode(200);
        backDataBean.setMsg("删除成功！");
        backDataBean.setResult(resultNum);
        responseEntity = ResponseEntity.status(HttpStatus.OK).body(backDataBean);

        return responseEntity;
    }

}
